using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Xml.Linq;
using System.Security.Cryptography;
using System.Text;
using System.IO;
namespace Criptografia
{
    public class Criptografia
    {

        /// <summary>
        /// Enncripta un string
        /// </summary>
        /// <param name="Pass"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        //public static string Encripta(string texto)
        //{
        //    byte[] clearTextBytes = Encoding.UTF8.GetBytes(texto);
        //    System.Security.Cryptography.SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
        //    MemoryStream ms = new MemoryStream();
        //    byte[] rgbIV = Encoding.ASCII.GetBytes("sadjfiasaewnsaae");
        //    byte[] key = Encoding.ASCII.GetBytes("sdfsfdasdoihfasfiusebnwbqweouiqw");
        //    CryptoStream cs = new CryptoStream(ms, rijn.CreateEncryptor(key, rgbIV), CryptoStreamMode.Write);
        //    cs.Write(clearTextBytes, 0, clearTextBytes.Length);
        //    cs.Close();
        //    return Convert.ToBase64String(ms.ToArray());
        //}

                /// <summary>
        /// Desencripta un string
        /// </summary>
        /// <param name="Pass"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        //public static string DesEncripta(string encriptado)
        //{
        //    byte[] encryptedTextBytes = Convert.FromBase64String(encriptado);
        //    MemoryStream ms = new MemoryStream();
        //    System.Security.Cryptography.SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
        //    byte[] rgbIV = Encoding.ASCII.GetBytes("sadjfiasaewnsaae");
        //    byte[] key = Encoding.ASCII.GetBytes("sdfsfdasdoihfasfiusebnwbqweouiqw");
        //    CryptoStream cs = new CryptoStream(ms, rijn.CreateDecryptor(key, rgbIV), CryptoStreamMode.Write);
        //    cs.Write(encryptedTextBytes, 0, encryptedTextBytes.Length);
        //    cs.Close();
        //    return Encoding.UTF8.GetString(ms.ToArray());
        //}




        public static byte[] Clave = Encoding.ASCII.GetBytes("miclaveunica");
        public static byte[] IV = Encoding.ASCII.GetBytes("Devjoker7.37hAES");

        public static string Encripta(string Cadena)
        {

            byte[] inputBytes = Encoding.ASCII.GetBytes(Cadena);
            byte[] encripted;
            RijndaelManaged cripto = new RijndaelManaged();
            using (MemoryStream ms = new MemoryStream(inputBytes.Length))
            {
                using (CryptoStream objCryptoStream = new CryptoStream(ms, cripto.CreateEncryptor(Clave, IV), CryptoStreamMode.Write))
                {
                    objCryptoStream.Write(inputBytes, 0, inputBytes.Length);
                    objCryptoStream.FlushFinalBlock();
                    objCryptoStream.Close();
                }
                encripted = ms.ToArray();
            }
            return Convert.ToBase64String(encripted);
        }



        public static string DesEncripta(string Cadena)
        {
            byte[] inputBytes = Convert.FromBase64String(Cadena);
            byte[] resultBytes = new byte[inputBytes.Length];
            string textoLimpio = String.Empty;
            RijndaelManaged cripto = new RijndaelManaged();
            using (MemoryStream ms = new MemoryStream(inputBytes))
            {
                using (CryptoStream objCryptoStream = new CryptoStream(ms, cripto.CreateDecryptor(Clave, IV), CryptoStreamMode.Read))
                {
                    using (StreamReader sr = new StreamReader(objCryptoStream, true))
                    {
                        textoLimpio = sr.ReadToEnd();
                    }
                }
            }
            return textoLimpio;
        }


    }
}
